package com.example.zx.theline.lyjh.lyrjh.pub;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zx.theline.R;
import com.example.zx.theline.lyjh.lyrjh.SgrjhActivity;
import com.example.zx.theline.lyjh.pub.JhshReadActivity;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.HashMap;
import java.util.Map;

import GsonBean.SgcxrjhDetailBean;
import GsonBean.ShyjhxqBean;
import GsonBean.SuccessBean;
import GsonBean.post.LjyyxRjdjhbShdw;
import Utils.MyToast;
import Utils.RefreshManager;
import app.MyApplication;
import base.BaseActivity;
import dialog.CustomDialog;
import network.NetWorkApi;
import network.ObjectHttpResponseHandler;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Jianqi on 2017\8\24 0024.
 *
 * @function 审核日计划界面点击详情进入页面
 */

public class SgrjhDetailActivity extends BaseActivity implements View.OnClickListener {
    //按钮
    private TextView leftTextView,centerTextView, cancel, scanShlc,sgrjh_sh;
    private LinearLayout layout_shlc;

    //计划内容
    private TextView lb, rjhh, ydbh, kssj,  xm, dd, hb, lc, xmmc, sgnr, sgjx, blfh, aqfx, fxcs;
    //相关单位
    private TextView sgdw, sgdwfzr, jldw, jldwfzr, jsdw, cwzd, ddt, dxjz, sbgldw, qtdw, shcs, xmzgbm;
    //Data
    private SgcxrjhDetailBean mDetailBean;
    private String bh;
    //日计划审核功能
    private CustomDialog customDialog;
    private TextView cancelBtn, confirmBtn; //弹出框按钮
    private RadioButton mAgreeBtn, mDisAgreeBtn, mAgreeUpdateBtn;
    private EditText mEditText;
    private LinearLayout blfh_layout;
    private TextView ckyjh;
    private String ycsTag = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_cxrjh);
        Intent intent = getIntent();
        bh = intent.getStringExtra("bh");
        ycsTag = intent.getStringExtra("yichaoshi");
        InitView();
        InitListen();
        InitData();
    }

    @Override
    protected void InitView() {
        centerTextView = (TextView) findViewById(R.id.centerTextView);
        cancel = (TextView) findViewById(R.id.cancel);
        layout_shlc = (LinearLayout) findViewById(R.id.layout_shlc);
        scanShlc = (TextView) findViewById(R.id.tv_scan_shlc);
        leftTextView = (TextView) findViewById(R.id.leftTextView);
        sgrjh_sh = (TextView) findViewById(R.id.sgrjh_sh);
        View view_blank = findViewById(R.id.view_blank);
        View view_blank1 = findViewById(R.id.view_blank1);

        lb = (TextView) findViewById(R.id.lb);
        rjhh = (TextView) findViewById(R.id.rjhh);
        ydbh = (TextView) findViewById(R.id.ydbh);

        kssj = (TextView) findViewById(R.id.kssj);

        xm = (TextView) findViewById(R.id.xm);
        dd = (TextView) findViewById(R.id.dd);
        hb = (TextView) findViewById(R.id.hb);
        lc = (TextView) findViewById(R.id.lc);
        xmmc = (TextView) findViewById(R.id.xmmc);
        sgnr = (TextView) findViewById(R.id.sgnr);
        sgjx = (TextView) findViewById(R.id.sgjx);
        blfh = (TextView) findViewById(R.id.blfh);
        blfh_layout = (LinearLayout) findViewById(R.id.blfh_layout);
        aqfx = (TextView) findViewById(R.id.aqfx);
        fxcs = (TextView) findViewById(R.id.fxcs);
        ckyjh = (TextView) findViewById(R.id.ckyjh);

        sgdw = (TextView) findViewById(R.id.sgdw);
        sgdwfzr = (TextView) findViewById(R.id.sgdwfzr);
        jldw = (TextView) findViewById(R.id.jldw);
        jldwfzr = (TextView) findViewById(R.id.jldwfzr);
        jsdw = (TextView) findViewById(R.id.jsdw);
        cwzd = (TextView) findViewById(R.id.cwzd);
        ddt = (TextView) findViewById(R.id.ddt);
        dxjz = (TextView) findViewById(R.id.dxjz);
        sbgldw = (TextView) findViewById(R.id.sbgldw);
        qtdw = (TextView) findViewById(R.id.qtdw);
        shcs = (TextView) findViewById(R.id.shcs);
        xmzgbm = (TextView) findViewById(R.id.xmzgbm);
        //审核的弹出框
        customDialog = new CustomDialog(this, R.layout.dialog_check_info, R.style.Theme_dialog);
        customDialog.setCancelable(false);
        cancelBtn = (TextView) customDialog.findViewById(R.id.btn_cancel);
        CheckBox mCheckBox = (CheckBox) customDialog.findViewById(R.id.checkbox);
        mCheckBox.setVisibility(View.GONE);
        confirmBtn = (TextView) customDialog.findViewById(R.id.btn_confirm);
        mAgreeBtn = (RadioButton) customDialog.findViewById(R.id.rb_agree);
        mAgreeUpdateBtn = (RadioButton) customDialog.findViewById(R.id.rb_agree_update);
        mAgreeUpdateBtn.setVisibility(View.GONE);
        mDisAgreeBtn = (RadioButton) customDialog.findViewById(R.id.rb_dis_agree);
        mEditText = (EditText) customDialog.findViewById(R.id.edit_check_info);
        mAgreeBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    mEditText.setVisibility(View.GONE);
                } else {
                    mEditText.setVisibility(View.VISIBLE);
                }
            }
        });
        if (ycsTag != null && ycsTag.equals("yichaoshi")) { //对于已超时计划来说
            layout_shlc.setVisibility(View.GONE);
            scanShlc.setVisibility(View.GONE);
            view_blank.setVisibility(View.INVISIBLE);
            view_blank1.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void InitData() {
        centerTextView.setText("日计划详情");
        requestDetailData();
    }

    @Override
    protected void InitListen() {
        cancel.setOnClickListener(this);
        layout_shlc.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);
        confirmBtn.setOnClickListener(this);
        scanShlc.setOnClickListener(this);
        leftTextView.setOnClickListener(this);
        ckyjh.setOnClickListener(this);
        sgrjh_sh.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                finish();
                break;
            case R.id.ckyjh: //查看月计划
                readMonthPlan();
                break;
            case R.id.tv_scan_shlc: //查看审核流程
                SgrjhCkshjlActivity.detailBean = mDetailBean;
                Intent intent = new Intent(this, SgrjhCkshjlActivity.class);
                startActivity(intent);
                break;
            case R.id.sgrjh_sh: //审核按钮
                customDialog.show();
                break;
            case R.id.btn_cancel: //审核时点击取消
                customDialog.dismiss();
                break;
            case R.id.btn_confirm: //审核时点击确定
                postSgrjhShData();
                break;
            case R.id.leftTextView:
                finish();
                break;
            default:
                break;
        }
    }


    //提交日计划审核操作的数据
    private void postSgrjhShData() {
        if (mAgreeUpdateBtn.isChecked() || mDisAgreeBtn.isChecked()) {
            if (mEditText.getText().toString().length() == 0) {
                Toast.makeText(SgrjhDetailActivity.this, "请填写审核意见", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        if (!mAgreeUpdateBtn.isChecked() && !mDisAgreeBtn.isChecked() && !mAgreeBtn.isChecked()) {
            Toast.makeText(SgrjhDetailActivity.this, "请选择是否通过", Toast.LENGTH_SHORT).show();
            return;
        }
        LjyyxRjdjhbShdw record = new LjyyxRjdjhbShdw();
        record.setRjhSgrq(mDetailBean.getInfo().getSgrq()); //施工日期
        record.setJdjhbBh(mDetailBean.getInfo().getBh()); //日计划号
        if (mAgreeBtn.isChecked()) { //是否通过
            record.setSftg("Y");
        }
        if (mDisAgreeBtn.isChecked()) { //不同意必须填写审核意见
            record.setSftg("N");
            record.setShyj(mEditText.getText().toString());
        }
        record.setShzt(mDetailBean.getInfo().getShzt()); //当前审核日计划的审核批次
        record.setShrlogin(MyApplication.getmLoginUser().getUserDisplayName()); //当前登录用户名
        record.setShrname(MyApplication.getmLoginUser().getTrueName()); //当前登录用户姓名
        record.setShrphone(MyApplication.getmLoginUser().getUserMobilephone()); //当前登录用户手机
        record.setDwId(MyApplication.getmLoginUser().getUserUnitId()); //当前登录单位id
        record.setShzt(mDetailBean.getInfo().getShzt()); //设置审核状态
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.postSgrjhShenheData(record, new Callback<SuccessBean>() {
                    @Override
                    public void onResponse(Call<SuccessBean> call, Response<SuccessBean> response) {
                        try {
                            responseData(response.body(), "审核成功", "审核失败");
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<SuccessBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    //处理响应数据
    private void responseData( SuccessBean mSuccessBean, String success, String failure) {
        if (mSuccessBean.getMsg() == null) {
            MyToast.showFail();

            return;
        }
        if (mSuccessBean.getMsg().equals("success")) {
            MyToast.showToast(success);
            RefreshManager.Sgrjh.RefreshShrjh();
            RefreshManager.Sgrjh.RefreshShywc();
            startActivity(new Intent(SgrjhDetailActivity.this, SgrjhActivity.class));
        } else {
            MyToast.showToast(failure);
        }
    }

    //请求查询日计划详情页面的数据
    private void requestDetailData() {
        Map<String,String> map=new HashMap<>();
        map.put("bh", bh);
        MyApplication.ShowWaitDialog(this);
        NetWorkApi.getChaxunrjhDetailData(map, new Callback<SgcxrjhDetailBean>() {
                    @Override
                    public void onResponse(Call<SgcxrjhDetailBean> call, Response<SgcxrjhDetailBean> response) {
                        try {
                            mDetailBean =response.body();
                            if (mDetailBean.getMsg().equals("success")) {
                                if (mDetailBean.getInfo() != null) { //有数据，渲染页面
                                    lb.setText(mDetailBean.getInfo().getLb());
                                    rjhh.setText(mDetailBean.getInfo().getBh());
                                    ydbh.setText(mDetailBean.getInfo().getYdbh());
                                    kssj.setText(mDetailBean.getInfo().getExsgrq());
                                    xm.setText(mDetailBean.getInfo().getXlxbList().get(0).getXlmc());
                                    dd.setText(mDetailBean.getInfo().getXlxbList().get(0).getSgdd());
                                    hb.setText(mDetailBean.getInfo().getXlxbList().get(0).getXbmc());
                                    lc.setText(mDetailBean.getInfo().getXlxbList().get(0).getLic());
                                    xmmc.setText(mDetailBean.getInfo().getXmmc());
                                    sgnr.setText(mDetailBean.getInfo().getNrmc());
                                    sgjx.setText(mDetailBean.getInfo().getJxmc());
                                    if (mDetailBean.getInfo().getLb().equals("C")) {
                                        blfh_layout.setVisibility(View.GONE);
                                    } else {
                                        blfh_layout.setVisibility(View.VISIBLE);
                                        blfh.setText(mDetailBean.getInfo().getBlfhmc());
                                    }
                                    aqfx.setText(mDetailBean.getInfo().getAqfxmc());
                                    fxcs.setText(mDetailBean.getInfo().getAqfxcsmc());

                                    sgdw.setText(mDetailBean.getInfo().getSgdw());
                                    sgdwfzr.setText(mDetailBean.getInfo().getSgdwfzr());
                                    jldw.setText(mDetailBean.getInfo().getJldw());
                                    jldwfzr.setText(mDetailBean.getInfo().getJldwfzr());
                                    jsdw.setText(mDetailBean.getInfo().getJsdw());
                                    cwzd.setText(mDetailBean.getInfo().getCwzd());
                                    ddt.setText(mDetailBean.getInfo().getDdt());
                                    dxjz.setText(mDetailBean.getInfo().getSgdxjz());
                                    sbgldw.setText(mDetailBean.getInfo().getSbgldw());
                                    qtdw.setText(mDetailBean.getInfo().getQtdw());
                                    shcs.setText(mDetailBean.getInfo().getShcs());
                                    xmzgbm.setText(mDetailBean.getInfo().getXmzgbm());

                                } else {
                                    Toast.makeText(SgrjhDetailActivity.this, "暂无更多数据", Toast.LENGTH_SHORT).show();
                                }
                            } else {
                                MyToast.showNullObjectError(SgrjhDetailActivity.this);
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<SgcxrjhDetailBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

    //查看月计划功能  根据日计划对应月计划编号查询月计划详情
    private void readMonthPlan() {
        MyApplication.ShowWaitDialog(this);

        Map<String, String> map = new HashMap<>();
        map.put("bh", mDetailBean.getInfo().getYdbh());
        NetWorkApi.getZsyjhXx(map, new Callback<ShyjhxqBean>() {
                    @Override
                    public void onResponse(Call<ShyjhxqBean> call, Response<ShyjhxqBean> response) {
                        try {
                            ShyjhxqBean mBean = response.body();
                            if (mBean.getMsg().equals("success")) {
                                JhshReadActivity.ZsyjhxxShBean = mBean;
                                startActivity(new Intent(SgrjhDetailActivity.this, JhshReadActivity.class));
                            }
                        } catch (Exception e) {
                            MyToast.showFail();
                        }
                        MyApplication.HideWaitDialog();
                    }

                    @Override
                    public void onFailure(Call<ShyjhxqBean> call, Throwable t) {
                        MyToast.showFail();
                    }
                }
        );
    }

}
